 
package com.zdkj.subway.rca.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.rca.entity.SwCkStatementCatReplenishTmp;
import com.zdkj.subway.rca.entity.SwCkStatementCatReplenish;
import com.zdkj.subway.rca.mapper.SwCkStatementCatReplenishTmpMapper;
import com.zdkj.subway.rca.service.SwCkStatementCatReplenishTmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.ZoneId;
import java.util.*;

/**
 * 长安通卡充资对账确认临时表（长安通-地铁）
 *
 * @author stw
 * @date 2020-10-24 18:03:22
 */
@Service
public class SwCkStatementCatReplenishTmpServiceImpl extends ServiceImpl<SwCkStatementCatReplenishTmpMapper, SwCkStatementCatReplenishTmp> implements SwCkStatementCatReplenishTmpService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SwCkStatementCatReplenishTmpServiceImpl.class);
    private String parsePattern = "yyyy.MM.dd";
    @Autowired
    private com.zdkj.subway.rca.service.SwCkStatementCatReplenishService swCkStatementCatReplenishService;

    @Override
    public Boolean saveBatchWithExcel(final File excelFile) throws Exception {
        List<Map<Integer, String>> listMap = EasyExcel.read(excelFile).sheet().headRowNumber(5).doReadSync();
        if (CollectionUtil.isNotEmpty(listMap)) {
            List<SwCkStatementCatReplenishTmp> listDb = new ArrayList<>();
            MultipartFileToFile.delteTempFile(excelFile);
            for (int m = 0; m < listMap.size(); m++) {
                Map<Integer, String> data = listMap.get(m);
                // 返回每条数据的键值对 表示所在的列 和所在列的值
                LOGGER.info("读取到数据:{}", JSON.toJSONString(data));

                SwCkStatementCatReplenishTmp tmpObj = new SwCkStatementCatReplenishTmp();
                tmpObj.setTradeDateTmp(data.get(0));//交易日期

                tmpObj.setReplenishXasubwayAmountTmp(data.get(1));
                tmpObj.setReplenishXasubwayMoneyTmp(data.get(2));
                tmpObj.setReplenishAirAmountTmp(data.get(3));
                tmpObj.setReplenishAirMoneyTmp(data.get(4));
                tmpObj.setReplenishXaztAmountTmp(data.get(5));
                tmpObj.setReplenishXaztMoneyTmp(data.get(6));
                
                /*
                tmpObj.setSalecardXasubwayAmountTmp(data.get(5));
                tmpObj.setSalecardXasubwayMoneyTmp(data.get(6));
                tmpObj.setSalecardAirAmountTmp(data.get(7));
                tmpObj.setSalecardAirMoneyTmp(data.get(8));
                */
                tmpObj.setTotalReplenishAmountTmp(data.get(7));
                tmpObj.setTotalReplenishMoneyTmp(data.get(8));
                /*
                tmpObj.setTotalSalecardAmountTmp(data.get(11));
                tmpObj.setTotalSalecardMoneyTmp(data.get(12));
                
                tmpObj.setTotalAmountTmp(data.get(13));
                tmpObj.setTotalMoneyTmp(data.get(14));
                */

                if (StringUtils.isEmpty(tmpObj.getTradeDateTmp())
                        || StringUtils.isEmpty(tmpObj.getTotalReplenishAmountTmp())
                        || StringUtils.isEmpty(tmpObj.getTotalReplenishMoneyTmp())
                        ) {
                    continue;
                }
                if (!parseDate(tmpObj.getTradeDateTmp())) {
                    continue;
                }

                final SwCkStatementCatReplenishTmp obj = ObjectUtils.getDefaultObj(SwCkStatementCatReplenishTmp.class);
                BeanUtils.copyProperties(tmpObj, obj, "deptId", "creatorId", "creatorName");
                checkErr(obj);
                listDb.add(obj);

            }
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put("CREATOR_ID", listDb.get(0).getCreatorId());
            this.baseMapper.deleteByMap(columnMap);
            return this.saveBatch(listDb);

        }

        return false;
    }

    private void checkErr(SwCkStatementCatReplenishTmp obj) {
        //更新字段
        String tradeDateTmp = obj.getTradeDateTmp();
        try {
            Date tradeDate = DateUtils.parseDate(tradeDateTmp, parsePattern);
            obj.setTradeDate(tradeDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        } catch (Exception e) {
            obj.setErrMsg("交易日期 为空或格式错误");
        }

        try {
            String replenishXasubwayAmountTmp = obj.getReplenishXasubwayAmountTmp();
            Long replenishXasubwayAmount = Long.parseLong(replenishXasubwayAmountTmp);
            obj.setReplenishXasubwayAmount(replenishXasubwayAmount);

            String replenishAirAmountTmp = obj.getReplenishAirAmountTmp();
            Long replenishAirAmount = Long.parseLong(replenishAirAmountTmp);
            obj.setReplenishAirAmount(replenishAirAmount);
            
            String replenishXaztAmountTmp = obj.getReplenishXaztAmountTmp();
            Long replenishXaztAmount = Long.parseLong(replenishXaztAmountTmp);
            obj.setReplenishXaztAmount(replenishXaztAmount);
            
            /*
            String salecardXasubwayAmountTmp = obj.getSalecardXasubwayAmountTmp();
            Long salecardXasubwayAmount = Long.parseLong(salecardXasubwayAmountTmp);
            obj.setSalecardXasubwayAmount(salecardXasubwayAmount);

            String salecardAirAmountTmp = obj.getSalecardAirAmountTmp();
            Long salecardAirAmount = Long.parseLong(salecardAirAmountTmp);
            obj.setSalecardAirAmount(salecardAirAmount);
            */
            String totalReplenishAmountTmp = obj.getTotalReplenishAmountTmp();
            Long totalReplenishAmount = Long.parseLong(totalReplenishAmountTmp);
            obj.setTotalReplenishAmount(totalReplenishAmount);
            /*
            String totalSalecardAmountTmp = obj.getTotalSalecardAmountTmp();
            Long totalSalecardAmount = Long.parseLong(totalSalecardAmountTmp);
            obj.setTotalSalecardAmount(totalSalecardAmount);

            String totalAmountTmp = obj.getTotalAmountTmp();
            Long totalAmount = Long.parseLong(totalAmountTmp);
            obj.setTotalAmount(totalAmount);
            */
        } catch (Exception e) {
            obj.setErrMsg("笔数 为空或格式错误");
        }

        try {
            BigDecimal num = new BigDecimal("100");

            String replenishXasubwayMoneyTmp = obj.getReplenishXasubwayMoneyTmp();
            BigDecimal replenishXasubwayMoney = new BigDecimal(replenishXasubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            replenishXasubwayMoney = replenishXasubwayMoney.multiply(num);
            obj.setReplenishXasubwayMoney(replenishXasubwayMoney.longValue());

            String replenishAirMoneyTmp = obj.getReplenishAirMoneyTmp();
            BigDecimal replenishAirMoney = new BigDecimal(replenishAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            replenishAirMoney = replenishAirMoney.multiply(num);
            obj.setReplenishAirMoney(replenishAirMoney.longValue());
            
            String replenishXaztMoneyTmp = obj.getReplenishXaztMoneyTmp();
            BigDecimal replenishXaztMoney = new BigDecimal(replenishXaztMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            replenishXaztMoney = replenishXaztMoney.multiply(num);
            obj.setReplenishXaztMoney(replenishXaztMoney.longValue());
            
            /*

            String salecardXasubwayMoneyTmp = obj.getSalecardXasubwayMoneyTmp();
            BigDecimal salecardXasubwayMoney = new BigDecimal(salecardXasubwayMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            salecardXasubwayMoney = salecardXasubwayMoney.multiply(num);
            obj.setSalecardXasubwayMoney(salecardXasubwayMoney.longValue());

            String salecardAirMoneyTmp = obj.getSalecardAirMoneyTmp();
            BigDecimal salecardAirMoney = new BigDecimal(salecardAirMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            salecardAirMoney = salecardAirMoney.multiply(num);
            obj.setSalecardAirMoney(salecardAirMoney.longValue());
            */
            String totalReplenishMoneyTmp = obj.getTotalReplenishMoneyTmp();
            BigDecimal totalReplenishMoney = new BigDecimal(totalReplenishMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            totalReplenishMoney = totalReplenishMoney.multiply(num);
            obj.setTotalReplenishMoney(totalReplenishMoney.longValue());
            /*
            String totalSalecardMoneyTmp = obj.getTotalSalecardMoneyTmp();
            BigDecimal totalSalecardMone = new BigDecimal(totalSalecardMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            totalSalecardMone = totalSalecardMone.multiply(num);
            obj.setTotalSalecardMoney(totalSalecardMone.longValue());

            String totalMoneyTmp = obj.getTotalMoneyTmp();
            BigDecimal totalMoney = new BigDecimal(totalMoneyTmp).setScale(2, BigDecimal.ROUND_HALF_UP);
            totalMoney = totalMoney.multiply(num);
            obj.setTotalMoney(totalMoney.longValue());
            */
        } catch (Exception e) {
            obj.setErrMsg("金额 为空或格式错误");
        }


        //判断数据是否存在
        SwCkStatementCatReplenish SwCkStatementCatReplenish = new SwCkStatementCatReplenish();
        BeanUtils.copyProperties(obj, SwCkStatementCatReplenish, "deptId","creatorId","creatorName","createTime");
        List<SwCkStatementCatReplenish> list = swCkStatementCatReplenishService.list(Wrappers.query(SwCkStatementCatReplenish));
        if (list != null && !list.isEmpty()) {
            obj.setErrMsg("数据已存在");
        }
    }

    private boolean parseDate(String string) {
        boolean convertSuccess = true;
        if (string == null) {
            return false;
        }

        try {
            DateUtils.parseDate(string, parsePattern);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    @Override
    public void saveBatchWithExcelOk(Integer creatorId) {
        //导入先删除
        this.baseMapper.deleteBatchWithExcelOk(creatorId);
        //导入先删除再添加包括重复的
        this.baseMapper.saveBatchWithExcelOkRepeat(creatorId);
        //this.baseMapper.saveBatchWithExcelOk(creatorId);
        saveBatchWithExcelCancel(creatorId);
    }

    @Override
    public void saveBatchWithExcelCancel(Integer creatorId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("CREATOR_ID", creatorId);
        this.baseMapper.deleteByMap(columnMap);
    }
}
